package com.hxzy.shop.commodity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.shop.commodity.dto.req.*;
import com.hxzy.shop.commodity.dto.resp.ClassResp;
import com.hxzy.shop.commodity.mapper.DrugInnerClassifiedManagementMapper;
import com.hxzy.shop.common.dto.commodity.req.ClassificationsReq;
import com.hxzy.shop.common.dto.commodity.resp.ClassifiedManagementResp;
import com.hxzy.shop.constant.AuthConstant;
import com.hxzy.shop.entity.ClassifiedManagement;
import com.hxzy.shop.commodity.mapper.ClassifiedManagementMapper;
import com.hxzy.shop.entity.DrugInnerClassifiedManagement;
import com.hxzy.shop.mapstruct.ClassifiedManagmentConvert;
import com.hxzy.shop.commodity.service.ClassifiedManagementService;
import com.hxzy.shop.utils.WebUtil;
import com.hxzy.shop.vo.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.function.Function;

/**
 * @Author wy
 * @Description 商品分类
 * @Date 2023/6/30 10:04
 * @Version 1.0
 */
@Service
public class ClassifiedManagementServiceImpl extends ServiceImpl<ClassifiedManagementMapper, ClassifiedManagement>  implements ClassifiedManagementService {

    @Autowired
    private ClassifiedManagementMapper classifiedManagementMapper;
    @Resource
    private DrugInnerClassifiedManagementMapper drugInnerClassifiedManagementMapper;

    /**
     * 获得药品的一级和二级
     *
     * @param id 药品的id
     * @return k 一级 v erj
     */
    @Override
    public ClassResp getClassifiedBigAndSmall(Long id) {
        return classifiedManagementMapper.getClassifiedBigAndSmall(id);
    }

    /**
     * 分页查询商品父子分类信息
     * @param req
     * @return
     */
    @Override
    public PageInfo<ClassifiedManagementResp> getClassifiedPage(ClassificationsReq req) {
        //商户的ID
        Long merchantId = Long.parseLong(WebUtil.getRequest().getHeader(AuthConstant.MERCHANT_ID));

        //按条件查询，只返回主分类的条件 (主分和子分类按照名称来查询，返回一定是主分类)
        PageInfo<ClassifiedManagement> pageInfo = PageHelper.startPage(req.getPageNum(),
                        req.getPageSize())
                .doSelectPageInfo(() -> classifiedManagementMapper.getClassifiedPage(req, merchantId));

        List<ClassifiedManagement> topRecores=pageInfo.getList();
        //没有顶级分类
        if(topRecores==null || topRecores.size()==0){
            return null;
        }

        //顶级分的id
        List<Long> topIds=topRecores.stream().map(ClassifiedManagement::getId).collect(Collectors.toList());
        // select id,name from Classified_Management where parent_id in (topsIds);
        //根据主分类查询子分类
        List<ClassifiedManagement> childList = super.list(new LambdaQueryWrapper<ClassifiedManagement>()
                .select(ClassifiedManagement::getId, ClassifiedManagement::getClassificationName, ClassifiedManagement::getParentId)
                .in(ClassifiedManagement::getParentId, topIds)
                .eq(ClassifiedManagement::getDisable,false));

        //合并主分类和子分类到某一个类下面
        List<ClassifiedManagementResp>  resultResp=topRecores.stream().map(p-> megerClassified(p,childList))
                .collect(Collectors.toList());

        //重新构造一个PageInfo分页对象
        PageInfo<ClassifiedManagementResp> respPageInfo= ClassifiedManagmentConvert.INSTANCE.convert(pageInfo);
        //重新赋值分页结果
        respPageInfo.setList(resultResp);

        return respPageInfo;
    }

    /**
     * 将子类封装到父类
     * @param classifiedManagement     当前父类
     * @param classifiedManagementList 子类集合
     * @return 封装好的子类
     */
    private ClassifiedManagementResp setSubClassified(ClassifiedManagement classifiedManagement,
                                                      List<ClassifiedManagement> classifiedManagementList) {
        List<ClassifiedManagementResp.SubClassified> list = new ArrayList<>();
        ClassifiedManagementResp classifiedManagementResp = new ClassifiedManagementResp();
        BeanUtil.copyProperties(classifiedManagement, classifiedManagementResp);
        //二级分类重新筛选组装
        classifiedManagementList.forEach(e -> {
            if (e.getParentId().equals(classifiedManagement.getId())) {
                ClassifiedManagementResp.SubClassified subClassified = new ClassifiedManagementResp.SubClassified();
                subClassified.setId(e.getId());
                subClassified.setName(e.getClassificationName());
                subClassified.setDrugNum(0);
                list.add(subClassified);
            }
        });
        // 保存
        classifiedManagementResp.setSubClassified(list);
        return classifiedManagementResp;
    }

    /**
     * 获得分类列表
     *
     * @return 分类列表
     */
    @Override
    public List<ClassifiedManagementResp> getClassifiedList() {
        return this.getClassifiedList2(null);
    }

    @Override
    public List<ClassifiedManagementResp> getClassifiedList2(String name) {
        List<ClassifiedManagement> list = this.list(new LambdaQueryWrapper<ClassifiedManagement>().
                select(ClassifiedManagement::getId, ClassifiedManagement::getClassificationName)
                .eq(ClassifiedManagement::getParentId, 0)
                .eq(ClassifiedManagement::getActivate, 1)
                .eq(ClassifiedManagement::getDisable, 0)
                .eq(ObjectUtil.isNotEmpty(name), ClassifiedManagement::getClassificationName, name)
        );

        if (list.size() > 0) {
            List<Long> collect = list.stream().map(ClassifiedManagement::getId).
                    collect(Collectors.toList());
            List<ClassifiedManagement> levelClassified = this.list(new LambdaQueryWrapper<ClassifiedManagement>()
                    .select(ClassifiedManagement::getClassificationName,
                            ClassifiedManagement::getId,
                            ClassifiedManagement::getParentId)
                    .in(ClassifiedManagement::getParentId, collect)
                    .eq(ClassifiedManagement::getDisable, 0));

            return list.stream().map(e -> setSubClassified(e, levelClassified)).collect(Collectors.toList());
        }

        return null;
    }

    @Override
    public List<ClassifiedManagementResp> getClassifiedManagement() {
        System.out.println("你正在获取Service -- getClassifiedManagement");
        List<ClassifiedManagementResp> classifiedManagementRespList = this.getClassifiedList();

        if(ObjectUtil.isEmpty(classifiedManagementRespList)){
            return null;
        }
        //获取未分类
        ClassifiedManagementResp classifiedManagementResp = new ClassifiedManagementResp();
        classifiedManagementResp.setId(0L);
        classifiedManagementResp.setParentId(0L);
        classifiedManagementResp.setClassificationName("未分类");
        classifiedManagementResp.setClassificationDescription("未分类");
        classifiedManagementResp.setActivate(true);
        classifiedManagementResp.setDisable(false);

        ClassifiedManagementResp.SubClassified subClassified = new ClassifiedManagementResp.SubClassified();
        List<ClassifiedManagementResp.SubClassified> subClassifiedList = new ArrayList<>();
        subClassifiedList.add(subClassified);
        classifiedManagementResp.setSubClassified(subClassifiedList);
        classifiedManagementRespList.add(classifiedManagementResp);

        //全部商品
        ClassifiedManagementResp allClassifiedManagementResp = new ClassifiedManagementResp();
        allClassifiedManagementResp.setId(1L);
        classifiedManagementResp.setParentId(0L);
        allClassifiedManagementResp.setClassificationName("全部商品");
        allClassifiedManagementResp.setClassificationDescription("全部商品");
        allClassifiedManagementResp.setActivate(true);
        allClassifiedManagementResp.setDisable(false);

        List<ClassifiedManagementResp.SubClassified> allSubClassified = new ArrayList<>();
        ClassifiedManagementResp.SubClassified allClassified = new ClassifiedManagementResp.SubClassified();
        allSubClassified.add(allClassified);
        allClassified.setName("全部商品");
        allClassified.setId(1L);
        allClassifiedManagementResp.setSubClassified(allSubClassified);
        classifiedManagementRespList.add(0,allClassifiedManagementResp);


        return classifiedManagementRespList;

    }

    /**
     * 合并主分类和子分类
     * @param parent 主分类
     * @param childList  所有子分类（包含其它）
     * @return
     */
    private ClassifiedManagementResp megerClassified(ClassifiedManagement parent, List<ClassifiedManagement> childList) {
        //主分类复制
        ClassifiedManagementResp resp = ClassifiedManagmentConvert.INSTANCE.convert(parent);
        //子分类集合
        List<ClassifiedManagementResp.SubClassified> childClassRespList = new ArrayList<>();
        //所有的子分类进行过滤
        childList.stream().filter(s -> s.getParentId().equals(parent.getId()))
                .forEach(s -> {
                    //内部类
                    ClassifiedManagementResp.SubClassified subClassified = new ClassifiedManagementResp.SubClassified();
                    subClassified.setId(s.getId());
                    subClassified.setName(s.getClassificationName());
                    childClassRespList.add(subClassified);
                });
        resp.setSubClassified(childClassRespList);

        return resp;
    }

    /**
     * 添加分类
     *
     * @param addClassifiedVo 添加分类请求类
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addClassified(AddClassifiedReq addClassifiedVo) {
        ClassifiedManagement classifiedManagement = null;
        if (!ObjectUtils.isEmpty(addClassifiedVo.getId()) && addClassifiedVo.getId() != null) {
            classifiedManagement = this.getById(addClassifiedVo.getId());
        }
        if (ObjectUtils.isNotEmpty(classifiedManagement)) {
            //删除该分类下的所有子分类
            for (SonClassified delete : addClassifiedVo.getDeleteSubClassified()) {
                LambdaQueryWrapper<ClassifiedManagement> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(ClassifiedManagement::getId, delete.getId());
                classifiedManagementMapper.delete(queryWrapper);
            }
            //添加子类到该分类下
            List<ClassifiedManagement> list = new ArrayList<>();
            for (SonClassified sonClassified : addClassifiedVo.getAddsubClassified()) {
                ClassifiedManagement sonCm = new ClassifiedManagement();
                sonCm.setClassificationName(sonClassified.getClassifiedName());
                sonCm.setParentId(addClassifiedVo.getId());
                sonCm.setSort(sonClassified.getSort());
                sonCm.setActivate(true);
                sonCm.setCreateTime(new Date());
                sonCm.setUpdateTime(new Date());
                sonCm.setDisable(false);
                list.add(sonCm);
            }
            //更新子分类
            for (SonClassified sonClassified : addClassifiedVo.getUpdateSubClassified()) {
                ClassifiedManagement son = classifiedManagementMapper.selectById(sonClassified.getId());
                son.setClassificationName(sonClassified.getClassifiedName());
                son.setSort(sonClassified.getSort());
                classifiedManagementMapper.updateById(son);
            }
            //更新该分类
            classifiedManagement.setClassificationName(addClassifiedVo.getClassifiedName());
            classifiedManagement.setSort(addClassifiedVo.getSort());
            classifiedManagement.setUpdateTime(new Date());
            classifiedManagement.setClassificationDescription(addClassifiedVo.getClassifiedDescribe());
            //更新分类
            this.updateById(classifiedManagement);
            //添加子分类
            return this.saveBatch(list);
        } else {
            //添加并保存父分类
            ClassifiedManagement cm = new ClassifiedManagement();
            cm.setClassificationName(addClassifiedVo.getClassifiedName());
            cm.setSort(addClassifiedVo.getSort());
            cm.setParentId(0L);
            cm.setClassificationDescription(addClassifiedVo.getClassifiedDescribe());
            cm.setActivate(true);
            cm.setCreateTime(new Date());
            cm.setUpdateTime(new Date());
            cm.setDisable(false);
            this.save(cm);
            //保存后将返回添加的父id
            Long parentId = cm.getId();
            // 添加子分类
            List<ClassifiedManagement> list = new ArrayList<>();
            for (SonClassified sonClassified : addClassifiedVo.getAddsubClassified()) {
                ClassifiedManagement sonCm = new ClassifiedManagement();
                sonCm.setClassificationName(sonClassified.getClassifiedName());
                sonCm.setParentId(parentId);
                sonCm.setSort(sonClassified.getSort());
                sonCm.setActivate(true);
                sonCm.setCreateTime(new Date());
                sonCm.setUpdateTime(new Date());
                sonCm.setDisable(false);
                list.add(sonCm);
            }
            return this.saveBatch(list);
        }
    }
    /**
     * 根据分类id 获得详细
     *
     * @param id 分类id
     * @return 获得当前分类的详细
     */
    @Override
    public ClassifiedManagementResp getClassified(Long id) {
        ClassifiedManagement byId = this.getById(id);
        List<ClassifiedManagement> managements = this.list(new LambdaQueryWrapper<ClassifiedManagement>()
                .select(ClassifiedManagement::getId, ClassifiedManagement::getClassificationName,
                        ClassifiedManagement::getParentId)
                .eq(ClassifiedManagement::getParentId, byId.getId()));
        return setSubClassified(byId, managements);
    }

    @Override
    public Object addNewClassified(AddNewClassifiedReq req) {
        int isOneOrTwoLevel = req.getIsOneOrTwoLevel();
        //判断1级还是2级分类
        if (isOneOrTwoLevel == 1) {
            //获得总1级分类条数
            List<ClassifiedManagement> classifiedManagements = classifiedManagementMapper.selectList(new LambdaQueryWrapper<ClassifiedManagement>()
                    .eq(ClassifiedManagement::getClassificationName, req.getClassifiedName())
                    .eq(ClassifiedManagement::getDisable,Boolean.FALSE).eq(ClassifiedManagement::getParentId,0));

            if(ObjectUtil.isNotEmpty(classifiedManagements)){
                throw new ApiException(500,"该1级分类已经存在！");
            }
            ClassifiedManagement classifiedManagement = new ClassifiedManagement();
            classifiedManagement.setClassificationName(req.getClassifiedName());
            classifiedManagement.setSort(0);
            classifiedManagement.setParentId(0L);
            classifiedManagement.setClassificationDescription(req.getClassifiedName());
            classifiedManagement.setActivate(true);
            classifiedManagement.setCreateTime(new Date());
            classifiedManagement.setUpdateTime(new Date());
            classifiedManagement.setDisable(false);
            classifiedManagementMapper.insert(classifiedManagement);
            return true;
        } else if (isOneOrTwoLevel == 2) {
            //判断是否有1级分类id
            String classifiedManagementId = req.getClassifiedManagementId();

            ClassifiedManagement classifiedManagement = classifiedManagementMapper.selectById(classifiedManagementId);

            if (ObjectUtil.isEmpty(classifiedManagement)) {
                throw new ApiException(500, "该一级分类不存在");
            }
            List<ClassifiedManagement> classifiedManagements = classifiedManagementMapper.selectList(new LambdaQueryWrapper<ClassifiedManagement>()
                    .eq(ClassifiedManagement::getClassificationName, req.getClassifiedName())
                    .eq(ClassifiedManagement::getDisable,Boolean.FALSE)
                    .eq(ClassifiedManagement::getParentId,classifiedManagementId));
            if(ObjectUtil.isNotEmpty(classifiedManagements)){
                throw new ApiException(500,"该1级分类下 新增的2级分类已经存在！");
            }

            ClassifiedManagement sonClassifiedManagement = new ClassifiedManagement();
            sonClassifiedManagement.setClassificationName(req.getClassifiedName());
            sonClassifiedManagement.setSort(0);
            sonClassifiedManagement.setParentId(classifiedManagement.getId());
            sonClassifiedManagement.setClassificationDescription(req.getClassifiedName());
            sonClassifiedManagement.setActivate(true);
            sonClassifiedManagement.setCreateTime(new Date());
            sonClassifiedManagement.setUpdateTime(new Date());
            sonClassifiedManagement.setDisable(false);
            classifiedManagementMapper.insert(sonClassifiedManagement);
            return true;
        }
        return false;
    }


    @Override
    public Object editClassified(EditClassifiedReq req) {

        Integer isOneOrTwoLevel = req.getIsOneOrTwoLevel();
        //判断1级还是2级分类
        if (isOneOrTwoLevel == 1) {
            Long classifiedManagementId = req.getClassifiedManagementId();
            ClassifiedManagement classifiedManagement = classifiedManagementMapper.selectById(classifiedManagementId);

            if(ObjectUtil.isNotEmpty(req.getClassifiedName())) {
                List<ClassifiedManagement> classifiedManagements = classifiedManagementMapper.selectList(new LambdaQueryWrapper<ClassifiedManagement>()
                        .eq(ClassifiedManagement::getClassificationName, req.getClassifiedName())
                        .eq(ClassifiedManagement::getDisable, Boolean.FALSE)
                        .eq(ClassifiedManagement::getParentId, 0)
                        .ne(ClassifiedManagement::getId, classifiedManagementId));

                if (ObjectUtil.isNotEmpty(classifiedManagements)) {
                    throw new ApiException(500, "该1级分类已经存在,无法修改！！");
                }
            }
            if (ObjectUtil.isEmpty(classifiedManagement)) {
                throw new ApiException(500, "分类不存在或已删除");
            }
            classifiedManagement.setClassificationName(req.getClassifiedName());
            classifiedManagementMapper.updateById(classifiedManagement);
            return true;
        } else if (isOneOrTwoLevel == 2) {

            Long classifiedManagementId = req.getSonClassifiedManagementId();
            ClassifiedManagement classifiedManagement = classifiedManagementMapper.selectById(classifiedManagementId);

            if (ObjectUtil.isEmpty(classifiedManagement)) {
                throw new ApiException(500, "分类不存在或已删除");
            }
            if(ObjectUtil.isNotEmpty(req.getClassifiedName())) {
                List<ClassifiedManagement> classifiedManagements = classifiedManagementMapper.selectList(new LambdaQueryWrapper<ClassifiedManagement>()
                        .eq(ClassifiedManagement::getClassificationName, req.getClassifiedName())
                        .eq(ClassifiedManagement::getDisable, Boolean.FALSE)
                        .eq(ClassifiedManagement::getParentId, req.getClassifiedManagementId())
                        .ne(ClassifiedManagement::getId,classifiedManagementId)
                        );

                if (ObjectUtil.isNotEmpty(classifiedManagements)) {
                    throw new ApiException(500, "该1级分类下此2级分类已经存在,无法修改！！");
                }
            }
            classifiedManagement.setClassificationName(req.getClassifiedName());
            classifiedManagement.setParentId(req.getClassifiedManagementId());
            classifiedManagementMapper.updateById(classifiedManagement);
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public Object deleteClassifiedById(ClassifiedIdReq req) {
        Long id = req.getClassifiedManagementId();
        ClassifiedManagement byId = this.getById(id);
        Long parentId = byId.getParentId();
        if(parentId==0){
            List<DrugInnerClassifiedManagement> drugInnerClassifiedManagements = drugInnerClassifiedManagementMapper.getNotDeleteDrugList(id,null);
            System.out.println(drugInnerClassifiedManagements);
            if(ObjectUtil.isNotEmpty(drugInnerClassifiedManagements)){
                throw new ApiException(500,"该分类下存在商品 无法删除该分类");
            }
        }else {
            List<DrugInnerClassifiedManagement> drugInnerClassifiedManagements = drugInnerClassifiedManagementMapper.getNotDeleteDrugList(null,id);

            System.out.println(drugInnerClassifiedManagements);
            if(ObjectUtil.isNotEmpty(drugInnerClassifiedManagements)){
                throw new ApiException(500,"该分类下存在商品 无法删除该分类");
            }
        }
        Long classifiedManagementId = req.getClassifiedManagementId();
        ClassifiedManagement classifiedManagement = classifiedManagementMapper.selectById(classifiedManagementId);
        if (ObjectUtil.isEmpty(classifiedManagement)) {
            throw new ApiException(500, "分类不存在或已删除");
        }
        //判断是1级还是2级分类
        if (classifiedManagement.getParentId() == 0) {
            //1级分类
            //删除当前分类
//            classifiedManagement.setDisable(Boolean.TRUE);
            classifiedManagementMapper.deleteById(classifiedManagement);
            List<ClassifiedManagement> classifiedManagements = classifiedManagementMapper.selectList(new LambdaQueryWrapper<ClassifiedManagement>()
                    .eq(ClassifiedManagement::getParentId, classifiedManagement.getId()));
            //删除子分类
            if (ObjectUtil.isNotEmpty(classifiedManagements)) {

                for (ClassifiedManagement c : classifiedManagements
                ) {
                    classifiedManagementMapper.deleteById(c);
                }
            }
        } else {
            //2级分类
            classifiedManagement.setDisable(Boolean.TRUE);
            classifiedManagementMapper.deleteById(classifiedManagement);
        }

        return true;
    }

    @Override
    public Object changeClassificationSort(ClassificationSortReq req) {
        List<String> classifiedManagementIdList = req.getClassifiedManagementIdList();

        if (ObjectUtil.isNotEmpty(classifiedManagementIdList)) {//1级分类
            this.changeSort(req.getClassifiedManagementIdList());
        }
        if (ObjectUtil.isNotEmpty(req.getSonclassifiedManagementIdList())) {//2级分类
            this.changeSort(req.getSonclassifiedManagementIdList());
        }
        return true;
    }

    /**
     * 处理排序
     */
    public Boolean changeSort(List<String> classifiedManagementIdList) {
        classifiedManagementIdList = classifiedManagementIdList.stream().filter(id -> ObjectUtil.isNotEmpty(id)).collect(Collectors.toList());
        List<ClassifiedManagement> classifiedManagements = classifiedManagementMapper.selectList(new LambdaQueryWrapper<ClassifiedManagement>().
                in(ClassifiedManagement::getId, classifiedManagementIdList));
        Map<Long, ClassifiedManagement> classifiedManagementMap = classifiedManagements.stream().collect(Collectors.toMap(ClassifiedManagement::getId, Function.identity()));

        int i = 0;
        for (String classification : classifiedManagementIdList) {
            ClassifiedManagement classifiedManagement = classifiedManagementMap.get(Long.valueOf(classification));
            if (ObjectUtil.isEmpty(classifiedManagement)||classification.equals("1")) {
                //主要是排除 未分类 和全部商品  这个分类的id
                continue;
            }
            classifiedManagement.setSort(i);
            classifiedManagementMapper.updateById(classifiedManagement);
            i++;
        }

        return true;
    }

    @Override
    public Boolean insert(ClassifiedManagement sonclassifiedManagement) {

        return classifiedManagementMapper.insert(sonclassifiedManagement)==1;
    }
    /**
     * 根据分类删除id
     *
     * @param id 需要删除分类的id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteClassified(Long id) {
        ClassifiedManagement byId = this.getById(id);
        Long parentId = byId.getParentId();
        if(parentId==0){
            List<DrugInnerClassifiedManagement> drugInnerClassifiedManagements = drugInnerClassifiedManagementMapper.getNotDeleteDrugList(id,null);
            if(ObjectUtil.isNotEmpty(drugInnerClassifiedManagements)){
                throw new ApiException(500,"该分类下存在商品 无法删除该分类");
            }
        }else {
            List<DrugInnerClassifiedManagement> drugInnerClassifiedManagements = drugInnerClassifiedManagementMapper.getNotDeleteDrugList(null,id);
            if(ObjectUtil.isNotEmpty(drugInnerClassifiedManagements)){
                throw new ApiException(500,"该分类下存在商品 无法删除该分类");
            }
        }
        boolean update = this.update(new LambdaUpdateWrapper<ClassifiedManagement>().set(ClassifiedManagement::getDisable, true).
                eq(ClassifiedManagement::getId, id));
        return update;
    }
    /**
     * 更新状态
     *
     * @param id    id
     * @param state 状态
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateState(List<Long> id, boolean state) {
        List<ClassifiedManagement> collect = id.stream().map(e -> {
            ClassifiedManagement classifiedManagement = new ClassifiedManagement();
            classifiedManagement.setId(e);
            classifiedManagement.setActivate(state);
            return classifiedManagement;
        }).collect(Collectors.toList());
        return this.updateBatchById(collect);
    }
}
